package com.mk.community.modules.bbs.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mk.community.common.api.CommonResult;
import com.mk.community.modules.bbs.dto.UserCreateParam;
import com.mk.community.modules.bbs.model.User;
import com.mk.community.modules.bbs.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 前端控制器
 * </p>
 * 接口定义规范：
 * 创建表记录：POST /{控制器路由名称}/create
 * 修改表记录：POST /{控制器路由名称}/update/{id}
 * 删除指定表记录：POST /{控制器路由名称}/delete/{id}
 * 分页查询表记录：GET /{控制器路由名称}/list
 * 获取指定记录详情：GET /{控制器路由名称}/{id}
 *
 * @author MagicKong
 * @since 2021-09-27
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/bbs/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation("根据ID查询一个用户")
    @GetMapping("/get/{id}")
    public CommonResult getUserById(@PathVariable("id") Integer id) {
        return CommonResult.success(userService.getById(id));
    }

    @ApiOperation("根据用户名查询一个用户")
    @GetMapping("/get/username/{username}")
    public CommonResult getUserByUsername(@PathVariable("username") String username) {
        return CommonResult.success(userService.getOne(new QueryWrapper<User>().eq("username", username)));
    }

    @ApiOperation("根据邮箱查询一个用户")
    @GetMapping("/get/email/{email}")
    public CommonResult getUserByEmail(@PathVariable("email") String email) {
        return CommonResult.success(userService.getOne(new QueryWrapper<User>().eq("email", email)));
    }

    @ApiOperation("插入一个用户")
    @PostMapping("/create")
    public CommonResult createUser(UserCreateParam userCreateParam) {
        User user = BeanUtil.toBean(userCreateParam, User.class);
        // TODO 加盐，加密
        user.setType(0);
        user.setStatus(0);
        user.setCreateTime(new Date());
        return CommonResult.success(userService.save(user));
    }

    @ApiOperation("根据ID修改用户的状态")
    @PostMapping("/update/status/{id}/{status}")
    public CommonResult updateUserStatus(@PathVariable("id") Integer id, @PathVariable("status") Integer status) {
        User user = new User();
        user.setId(id);
        user.setStatus(status);
        return CommonResult.success(userService.updateById(user));
    }

    @ApiOperation("根据ID修改用户的头像路径")
    @PostMapping("/update/headerUrl/{id}/{headerUrl}")
    public CommonResult updateUserHeaderUrl(@PathVariable("id") Integer id, @PathVariable("headerUrl") String headerUrl) {
        User user = new User();
        user.setId(id);
        user.setHeaderUrl(headerUrl);
        return CommonResult.success(userService.updateById(user));
    }

    @ApiOperation("根据ID修改用户的密码")
    @PostMapping("/update/password/{id}/{password}")
    public CommonResult updateUserPassword(@PathVariable("id") Integer id, @PathVariable("password") String password) {
        User user = new User();
        user.setId(id);
        user.setPassword(password);
        return CommonResult.success(userService.updateById(user));
    }
}

